#!/bin/bash
#   d-devlibdeps -- get list of build-time dependencies
#   Copyright (C) 2002-2005 Junichi Uekawa
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# 2002 May 3. Created.
#   automatic depends line generator.

set -e
set -o pipefail

eval "$(dpkg-architecture -s)"

getname() {
	local SONAMELIBNAME
	local SONAMEVERSION
	local SONAME
	SONAME="$1"
	SONAMELIBNAME=${SONAME/%.so*}
	SONAMEVERSION=${SONAME/#*.so.}
	case "$SONAMELIBNAME" in
	  *[0-9])
		RETURN="$SONAMELIBNAME-$SONAMEVERSION"
		;;
	  *)
		RETURN="$SONAMELIBNAME$SONAMEVERSION"
		;;
	esac
}

overridedevlibdeps() {
	# overrides necessary until the scheme is adopted.
	sed \
		"${OVERRIDE[@]/#/-e}" \
		-e 's/libclibc\.so-dev//' \
		-e 's/ld1-dev//' \
		-e 's/ld64-[12]-dev//' \
		-e 's/ld-\(k\?freebsd\|linux\).*[0-9]-dev//' \
		-e 's/lib\(avcodec\|avdevice\|avfilter\|avformat\|avresample\|avutil\|postproc\|swresample\|swscale\)\(-ffmpeg\)\?[0-9]\+-dev/lib\1-dev/' \
		-e 's/lib\(cdt\|cgraph\|gvpr\|pathplan\|xdot\)[0-9][^-]*-dev/libgraphviz-dev/' \
		-e 's/libanl1-dev//' \
		-e 's/libatk-1\.0-0-dev/libatk1.0-dev/' \
		-e 's/libatomic1-dev//' \
		-e 's/libavahi-client.\?-dev/libavahi-client-dev/' \
		-e 's/libavahi-common.\?-dev/libavahi-common-dev/' \
		-e 's/libavahi-glib.\?-dev/libavahi-glib-dev/' \
		-e 's/libblas3-dev/libblas-dev/' \
		-e 's/libboost_\([a-z0-9.]\+\)\.[0-9]-dev/libboost-\1-dev/' \
		-e 's/libboost_\([a-z0-9.]\+\)_\([a-z0-9.]\+\)\.[0-9]-dev/libboost-\1-\2-dev/' \
		-e 's/libbpp-\([-a-z]\+\)[0-9]*-dev/libbpp-\1-dev/' \
		-e 's/libbz2-1.0-dev/libbz2-dev/' \
		-e 's/libc[0-9][.0-9]*-dev//' \
		-e 's/libcairo-gobject2-dev/libcairo-dev/' \
		-e 's/libcblas3-dev/libcblas-dev/' \
		-e 's/libcelt0-0-dev/libcelt-dev/' \
		-e 's/libcholmod[.0-9]\+-dev/libsuitesparse-dev/' \
		-e 's/libcomerr2//' \
		-e 's/libcom_err2-dev/comerr-dev/' \
		-e 's/libcrack2-dev/cracklib2-dev/' \
		-e 's/libcrypt1\.1-dev//' \
		-e 's/libcrypt1-dev//' \
		-e 's/libcrypto.*-dev/libssl-dev/' \
		-e 's/libcurl4-dev/libcurl4-openssl-dev/' \
		-e 's/libcurl-gnutls4-dev/libcurl4-gnutls-dev/' \
		-e 's/libctpp2-.*-dev/libctpp2-dev/' \
		-e 's/libdbus-1-3-dev/libdbus-1-dev/' \
		-e 's/libdcm\(data\|net\)[0-9]\+-dev/libdcmtk-dev/' \
		-e 's/libdeflate[.0-9]\+-dev/libdeflate-dev/' \
		-e 's/libdl2\.1-dev//' \
		-e 's/libdl2-dev//' \
		-e 's/libeditorconfig0-dev/libeditorconfig-dev/' \
		-e 's/libeXosip2-[0-9]\+-dev/libexosip2-dev/' \
		-e 's/libf2clibf2c.so-dev/libf2c2-dev/' \
		-e 's/libffado2-dev/libffado-dev/' \
		-e 's/libfml[.0-9]\+-dev/libfml-dev/' \
		-e 's/libfribidi0-dev/libfribidi-dev/' \
		-e 's/libGenome-\([.0-9]\+\)-[0-9]\+-dev/libgenome-\1-dev/' \
		-e 's/libGenome[.0-9]\+-dev/libgenome-dev/' \
		-e 's/libGL1-dev/xlibmesa-gl-dev/' \
		-e 's/libGLU1-dev/xlibmesa-glu-dev/' \
		-e 's/libgcc_s.*-dev//' \
		-e 's/libgconf-2-4-dev/libgconf2-dev/' \
		-e 's/libgd[23]-dev/libgd-dev/' \
		-e 's/libgdk-1\.2-0-dev/libgtk1.2-dev/' \
		-e 's/libgdk-3-0-dev/libgtk-3-dev/' \
		-e 's/libgdk_pixbuf-2\.0-0-dev/libgtk2.0-dev/' \
		-e 's/libgdk-x11-2\.0-0-dev/libgtk2.0-dev/' \
		-e 's/libgfortran[345]-dev/libgfortran-10-dev/' \
		-e 's/libgif7-dev/libgif-dev/' \
		-e 's/libgio-2\.0-0-dev/libglib2.0-dev/' \
		-e 's/libgioftp2-dev/libfontforge-dev/' \
		-e 's/libglib-1\.2-0-dev/libglib1.2-dev/' \
		-e 's/libglib-2\.0-0-dev/libglib2.0-dev/' \
		-e 's/libglpk[.0-9]\+-dev/libglpk-dev/' \
		-e 's/libgmodule-1\.2-0-dev/libglib1.2-dev/' \
		-e 's/libgmodule-2\.0-0-dev/libglib2.0-dev/' \
		-e 's/libgmp10-dev/libgmp-dev/' \
		-e 's/libgnutls26/libgnutls/' \
		-e 's/libgnutls\(-deb0-28\|30\)/libgnutls28/' \
		-e 's/libgobject-2\.0-0-dev/libglib2.0-dev/' \
		-e 's/libgomp1-dev//' \
		-e 's/libgsl23-dev/libgsl-dev/' \
		-e 's/libgssapi_krb5-2-dev/libkrb5-dev/' \
		-e 's/libgthread-1\.2-0-dev/libglib1.2-dev/' \
		-e 's/libgthread-2\.0-0-dev/libglib2.0-dev/' \
		-e 's/libgtk-1\.2-0-dev/libgtk1.2-dev/' \
		-e 's/libgtk-3-0-dev/libgtk-3-dev/' \
		-e 's/libgtk-x11-2\.0-0-dev/libgtk2.0-dev/' \
		-e 's/libgunicode[0-9]-dev/libfontforge-dev/' \
		-e 's/libgutils[0-9]-dev/libfontforge-dev/' \
		-e 's/libharfbuzz[0-9.]\+-dev/libharfbuzz-dev/' \
		-e 's/libhts[1-9]-dev/libhts-dev/' \
		-e 's/libhtscodecs[.0-9]\+-dev/libhtscodecs-dev/' \
		-e 's/libhdf5_serial.*-dev/libhdf5-dev/' \
		-e 's/libhunspell-[0-9.-]\+-dev/libhunspell-dev/' \
		-e 's/libICE6-dev/libice-dev/' \
		-e 's/libicu\(i18n\|uc\)[0-9]\+-dev/libicu-dev/' \
		-e 's/libijs.*-dev/libijs-dev/' \
		-e 's/libjackserver0-dev//' \
		-e 's/libjasper1/libjasper/' \
		-e 's/libjpeg[0-9.]\+-dev/libjpeg-dev/' \
		-e 's/libjsoncpp[.0-9]\+-dev/libjsoncpp-dev/' \
		-e 's/libk5crypto3-dev/libkrb5-dev/' \
		-e 's/libkrb1-dev/kerberos4kth-dev/' \
		-e 's/libkrb5-3-dev/libkrb5-dev/' \
		-e 's/liblapack3-dev/liblapack-dev/' \
		-e 's/liblcms2-2-dev/liblcms2-dev/' \
		-e 's/liblept5-dev/libleptonica-dev/' \
		-e 's/libleveldb1-dev/libleveldb-dev/' \
		-e 's/libmaus2_.*-dev/libmaus2-dev/' \
		-e 's/lib\(nspr\|plc\|plds\)4-lib\(nspr\|plc\|plds\)4.so-dev/libnspr4-dev/' \
		-e 's/lib\(nss\|nssutil\|smime\)3-lib\(nss\|nssutil\|smime\)3.so-dev/libnss3-dev/' \
		-e 's/libre[.0-9]\+-dev/libre-dev/' \
		-e 's/librem[.0-9]\+-dev/librem-dev/' \
		-e 's/liblzma5-dev/liblzma-dev/' \
		-e 's/libls1-dev//' \
		-e 's/libMUSCLE-\([.0-9]\+\)-[0-9]\+-dev/libmuscle-\1-dev/' \
		-e 's/libMUSCLE[.0-9]\+-dev/libmuscle-dev/' \
		-e 's/libm1-dev//' \
		-e 's/libm6\.1-dev//' \
		-e 's/libm6-dev//' \
		-e 's/libmbed[.a-z0-9-]\+-dev/libmbedtls-dev/' \
		-e 's/libminisat2-dev/minisat/' \
		-e 's/libncurses6-dev/libncurses-dev/' \
		-e 's/libnettle[.0-9]*-dev/nettle-dev/' \
		-e 's/libnsl1\.1-dev//' \
		-e 's/libnsl1-dev//' \
		-e 's/libof\(log\|std\)[0-9]\+-dev/libdcmtk-dev/' \
		-e 's/libogg0-dev/libogg-dev/' \
		-e 's/libopencore-\(amr[nw]b\)[0-9]\+-dev/libopencore-\1-dev/' \
		-e 's/libopus0-dev/libopus-dev/' \
		-e 's/libosip\(parser\)\?2-[0-9]\+-dev/libosip2-dev/' \
		-e 's/libpam0-dev/libpam0g-dev/' \
		-e 's/libpango\(\|cairo\|ft2\|x\|xft\)-1\.0-0-dev/libpango1.0-dev/' \
		-e 's/libpaper1/libpaper/' \
		-e 's/libparted-1\.6-0-dev/libparted1.6-dev/' \
		-e 's/libpbbam[.0-9]\+-dev/libpbbam-dev/' \
		-e 's/libpbcopper[.0-9]\+-dev/libpbcopper-dev/' \
		-e 's/libpcre2-[0-9-]\+-dev/libpcre2-dev/' \
		-e 's/libpj\(\|lib-util\|media\(\|-audiodev\|-codec\|-videodev\)\|nath\|sip\(\|-simple\|-ua\)\|sua\)2-dev/libpjproject-dev/' \
		-e 's/libpng16-16-dev/libpng-dev/' \
		-e 's/libpopt0-dev/libpopt-dev/' \
		-e 's/libportaudio2-dev/portaudio19-dev/' \
		-e 's/libProgressBar0-dev/libgkarrays-dev/' \
		-e 's/libprotobuf[0-9]\+-dev/libprotobuf-dev/' \
		-e 's/libpugixml.*-dev/libpugixml-dev/' \
		-e 's/libpthread0\.3-dev//' \
		-e 's/libpthread0-dev//' \
		-e 's/libpthread-stubs0-dev//' \
		-e 's/libpvm3-3-dev/pvm-dev/' \
		-e 's/libpython2.7-1\.0-dev/libpython-dev/' \
		-e 's/libpython3\.\([.0-9]\+\)m\?[-.0-9]\+-dev/libpython3.\1-dev/' \
		-e 's/libQtCore4-dev/libqt4-dev/' \
		-e 's/libQtGui4-dev/libqt4-dev/' \
		-e 's/libQt5\(\|Core\|Gui\|Widgets\)5-dev/qtdeclarative5-dev/' \
		-e 's/libquadmath0-dev//' \
		-e 's/libraptor2-0-dev/libraptor2-dev/' \
		-e 's/libraw1394-8-dev/libraw1394-dev/' \
		-e 's/libreadline[7-8]-dev/libreadline-dev/' \
		-e 's/libresolv[.0-9]\+-dev//' \
		-e 's/librsvg-2-2-dev/librsvg2-dev/' \
		-e 's/librt1-dev//' \
		-e 's/libSDL-1\.2-0-dev/libsdl1.2-dev/' \
		-e 's/libSDL2-[0-9.-]\+-dev/libsdl2-dev/' \
		-e 's/libSM6-dev/libsm-dev/' \
		-e 's/libsecp256k1-0-dev/libsecp256k1-dev/' \
		-e 's/libsipwitch.*-dev/libsipwitch-dev/' \
		-e 's/libspeex1-dev/libspeex-dev/' \
		-e 's/libspeexdsp1-dev/libspeexdsp-dev/' \
		-e 's/libspiro[0-9]\+-dev/libspiro-dev/' \
		-e 's/libsqlite3-[.0-9]\+-dev/libsqlite3-dev/' \
		-e 's/libssl.*-dev/libssl-dev/' \
		-e 's/libssw[.0-9]\+-dev/libssw-dev/' \
		-e 's/libsrtp0/libsrtp/' \
		-e 's/libstdc++6.*-dev//' \
		-e 's/libsvm3-dev/libsvm-dev/' \
		-e 's/libtheora0-dev/libtheora-dev/' \
		-e 's/libtinfo6-dev/libtinfo-dev/' \
		-e 's/libtinyxml[.0-9]\+-dev/libtinyxml-dev/' \
		-e 's/libtrio[12]-dev/libtrio-dev/' \
		-e 's/libu\(common\|secure\)[0-9]*-dev/libucommon-dev/' \
		-e 's/libuninameslist1-dev/libuninameslist-dev/' \
		-e 's/libutil1-dev//' \
		-e 's/libuuid1-dev/uuid-dev/' \
		-e 's/libv4l2-0-dev/libv4l-dev/' \
		-e 's/libv8-[0-9.]*-dev/libv8-dev/' \
		-e 's/libvo-amrwbenc0-dev/libvo-amrwbenc-dev/' \
		-e 's/libvorbis0-dev/libvorbis-dev/' \
		-e 's/libvorbisenc2-dev/libvorbis-dev/' \
		-e 's/libwoff2.*-dev/libwoff-dev/' \
		-e 's/libX11-6-dev/libx11-dev/' \
		-e 's/libX\([a-z]\+\)[0-9]-dev/libx\1-dev/' \
		-e 's/libxml2-2-dev/libxml2-dev/' \
		-e 's/libz1-dev/zlib1g-dev/' \
		-e 's/libznz[0-9.]*-dev/libznz-dev/' \
		-e 's/libzstd[.0-9.]*-dev/libzstd-dev/' \
		-e 's/xlibmesa-gl-dev/libgl1-mesa-dev/' \
		"${OVERRIDE[@]/#/-e}"
}


validate_package() {
# validate if package foo exists, this relies on apt.
	if [ -z "$1" ]; then
		# ignore empty.
		return;
	fi
	if grep "^Package: $1\$" debian/control > /dev/null 2> /dev/null; then
		# ignore package that is going to be created.
		echo " --> $1 package from same source package." >&2
		return
	fi
	case $(apt-cache show "$1" 2> /dev/null | wc -l) in
	  0)
		case $(apt-cache showpkg "$1" 2> /dev/null \
		| awk ' /^Reverse Provides: $/,/^$/ {if ($0 !~ /:/) {print $0}}' \
		| wc -l) in
		  0)
			echo "devlibs error: There is no package matching [$1] and noone provides it, please report bug to d-shlibs maintainer" >&2
			return 1
			;;
		  *)
			echo " --> $1 is provided by a package." >&2
			;;
		esac
		;;
	  *)
		echo " --> $1 package exists." >&2
		;;
	esac
	return 0;
}

while true; do
	case "$1" in
	  --override=*)
		OVERRIDE[${#OVERRIDE[@]}]="${1/--override=/}"
		shift
		;;
	  --override)
		OVERRIDE[${#OVERRIDE[@]}]="$2"
		shift 2
		;;
	  --)
		shift
		break
		;;
	  --*)
		echo "$0: Unknown option $1" > /dev/stderr
		exit 64 # EX_USAGE: command line usage error
		;;
	  *)
		break
		;;
    esac
done

if [ -z "$2" ]; then
	echo "$0: Please read manpage for help, not enough options given"
	exit 1
fi

OUTPUTFILE="$1"
shift;

DEPENDS=""
for A in "$@"; do
	# let shell do the delimiting of objdump output./
	DEPENDS="${DEPENDS:+$DEPENDS }$(set -o pipefail; "$DEB_HOST_GNU_TYPE-objdump" -p "$A" \
	| sed -n 's/^.*NEEDED *//p')"
done

outputtmp=$(mktemp)
(
	set -e
	if [ -f "$OUTPUTFILE" ]; then
		if grep -v "^devlibs:Depends=" "$OUTPUTFILE"; then
			:
		fi
	fi
	printf "devlibs:Depends="
	for A in $DEPENDS; do #DEPENDS is a space-delimited thing.
		getname "$A"
		echo "$RETURN-dev" | overridedevlibdeps
	done \
	| LC_ALL=C sort \
	| uniq \
	| while read -r B B_alt; do
		if validate_package "$B"; then
			printf "%s " "$B $B_alt";
		else
			echo "ERROR_DEVLIB "
		fi
	done \
	| sed -e 's/\([a-z0-9)]\) \+\([a-z]\)/\1, \2/g' -e 's/ \+/ /g' -e 's/^ //' -e 's/ $//'
	printf '\n'
) > "$outputtmp"

if grep "ERROR_DEVLIB" "$outputtmp" > /dev/null; then
	exit 1
fi

mv "$outputtmp" "$OUTPUTFILE"
